<template>
  <div class="mod-config">
    <el-tabs value="second" type="card">
      <el-tab-pane label="按广告" name="second">
        <scounttheme2></scounttheme2>
      </el-tab-pane>
      <el-tab-pane label="按终端" name="first">
        <el-form size="mini" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-col :span="3">
            <el-form-item>
              <el-input v-model="dataForm.themeName" placeholder="节目名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-input v-model="dataForm.mac" placeholder="MAC" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-select v-model="dataForm.groupBy" placeholder="统计方式">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item>
              <el-date-picker
                v-model="dataForm.value7"
                type="datetimerange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions2">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <!--<el-button v-if="isAuth('sys:scounttheme:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
            <!--<el-button v-if="isAuth('sys:scounttheme:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
          </el-form-item>
        </el-form>
        <el-table
          size="mini"
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <!--<el-table-column-->
          <!--type="selection"-->
          <!--header-align="center"-->
          <!--align="center"-->
          <!--width="50">-->
          <!--</el-table-column>-->
          <!--<el-table-column-->
          <!--prop="id"-->
          <!--header-align="center"-->
          <!--align="center"-->
          <!--label="id"-->
          <!--:width="80">-->
          <!--</el-table-column>-->
          <el-table-column
            prop="themeName"
            header-align="center"
            align="center"
            label="节目名称">
          </el-table-column>
          <el-table-column
            prop="mac"
            header-align="center"
            align="center"
            label="MAC">
          </el-table-column>
          <el-table-column
            prop="groupTime"
            header-align="center"
            align="center"
            label="统计时间">
          </el-table-column>
          <el-table-column
            prop="count"
            header-align="center"
            align="center"
            label="播放次数">
          </el-table-column>
          <!--<el-table-column-->
          <!--fixed="right"-->
          <!--header-align="center"-->
          <!--align="center"-->
          <!--width="150"-->
          <!--label="操作">-->
          <!--<template slot-scope="scope">-->
          <!--<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
          <!--</template>-->
          <!--</el-table-column>-->
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script>
  import AddOrUpdate from './scounttheme-add-or-update'
  import Scounttheme2 from './scounttheme2'
  export default {
    data () {
      return {
        dataForm: {
          themeName: '',
          mac: '',
          groupBy: 1,
          value7: ''
        },
        options: [
          {
            value: 1,
            label: '按日'
          }, {
            value: 2,
            label: '按周'
          }, {
            value: 3,
            label: '按月'
          }],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        pickerOptions2: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      }
    },
    components: {
      Scounttheme2,
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        var time = this.dataForm.value7
        var startTime = ''
        var endTime = ''
        if (time !== null && time.length !== 0) {
          startTime = time[0]
          endTime = time[1]
          console.log(startTime + 'ggggg' + endTime)
        }
        this.$http({
          url: this.$http.adornUrl('/sys/scounttheme/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'themeName': this.dataForm.themeName != null ? this.dataForm.themeName.trim() : '',
            'groupBy': this.dataForm.groupBy,
            'mac': this.dataForm.mac != null ? this.dataForm.mac.trim() : '',
            'startTime': startTime,
            'endTime': endTime
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/scounttheme/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
